#include <iostream>
#include <vector>
#include <queue>
using namespace std;

struct edge {
    int to;
    edge* next;
    edge() : to(-1),next(nullptr){}
    edge(int x) : to(x), next(nullptr) {}
};

edge vertex[100003];
edge* tail[100003];
bool v[100003];
int indegree[100003];

bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
    int n = numCourses;
    for (int i = 0; i < n; i++) tail[i] = &vertex[i];
    for(int i=0;i<prerequisites.size();i++){
        int to = prerequisites[i][0];
        int from = prerequisites[i][1];
        tail[from]->next = new edge(to);
        tail[from] = tail[from]->next;
        indegree[to]++;
    }

    queue<int> Q;
    int cnt = 0;
    for(int i=0;i<n;i++){
        if(indegree[i] == 0){
            Q.push(i);
            cnt++;
        }
    }
    while(!Q.empty()){
        edge* p = &vertex[Q.front()];
        Q.pop();
        while(p->next){
            p = p->next;
            indegree[p->to]--;
            if(indegree[p->to] == 0){
                Q.push(p->to);
                cnt++;
            }
        }
    }
    if(cnt == n) return 1;
    return 0;
}